/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import com.google.gson.Gson;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Booking;
import model.Receipt;

/**
 *
 * @author Jorge
 */
public class ConcreteStorageService extends ContreteService implements StorageService{

    public ConcreteStorageService() {
        super("http://localhost:8084/WebServiceFlightBooking/webresources/storage");
    }

    
    @Override
    public Receipt generateReceipt(Booking booking) {
        try {
            URL recurso = new URL(urlBase);
            HttpURLConnection conexao = (HttpURLConnection) recurso.openConnection();
            conexao.setRequestMethod("PUT");
            conexao.setRequestProperty("content-type", "application/json");
            conexao.setDoOutput(true);
            
            String envia = new Gson().toJson(booking).toString();
            
            
            conexao.connect();
            conexao.getOutputStream().write(envia.getBytes(), 0, envia.length());
            
            String res = lerInputStream(conexao.getInputStream());
            conexao.disconnect();
            
            Receipt receipt = new Gson().fromJson(res, Receipt.class);
            
            return receipt;
        } catch (IOException ex) {
            Logger.getLogger(ConcreteStorageService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } 
    }
    
}
